package com.communal.web.form;


import com.communal.model.ModelEntity;

import net.sf.ezmorph.bean.MorphDynaBean;
import net.sf.json.JSONObject;

/**
 * Base form with common fields <br>
 * @author Created by andrey.kuznetsov <br>
 * @author Last modified by $Author$ <br>
 * @author Last modified on $Date$ at revision $Revision$ <br>
 */
public class BaseForm {

	public static final String ID = "id";
	public static final String ENABLED = "enabled";
	public static final String MODEL_ID = "modelId";
	
	protected Integer id;
	protected Short enabled;
	protected Integer modelId;

	/**
	 * Constructor from JSON data for {@link BaseForm} <br>
	 * @param p_data - json data; {@link Object}
	 * @param p_constructorFlag - flag for constructor can be true or false
	 */
	public BaseForm(Object p_data, boolean p_constructorFlag) {
		JSONObject jsonObject = JSONObject.fromObject(p_data);
		MorphDynaBean bean = (MorphDynaBean) JSONObject.toBean(jsonObject);
		parseBean( bean );
	}
	
	private void parseBean(MorphDynaBean bean){
		id = Integer.parseInt(bean.get(ID).toString());
		enabled = Short.parseShort(bean.get(ENABLED).toString());
		modelId = Integer.parseInt(bean.get(MODEL_ID).toString());
	}
	
	/**
	 * Constructor for {@link BaseForm} from model entity <br>
	 * @param p_money - money pojo; {@link IMoney}
	 */
	public BaseForm(ModelEntity p_entity) {
		id = p_entity.getId();
		enabled = p_entity.getEnabled();
		modelId = p_entity.getModel().getId();
	}
	
	/**
	 * Returns unique id of money record <br>
	 * @return {@link Integer}
	 */
	public Integer getId() {
		return id;
	}
	
	/**
	 * Sets unique id of money record <br>
	 * @param p_id {@link Integer}
	 */
	public void setId(Integer p_id) {
		id = p_id;
	}
	
	/**
	 * Returns enabled flag <br>
	 * @return {@link Short}
	 */
	public Short getEnabled() {
		return enabled;
	}
	
	/**
	 * Sets enabled flag <br>
	 * @param p_enabled; {@link Short}
	 */
	public void setEnabled(Short p_enabled) {
		enabled = p_enabled;
	}
	
	/**
	 * Returns unique id of model <br>
	 * @return {@link Integer}
	 */
	public Integer getModelId() {
		return modelId;
	}
	
	/**
	 * Sets unique id of model <br>
	 * @param p_modelId; {@link Integer}
	 */
	public void setModelId(Integer p_modelId) {
		modelId = p_modelId;
	}
}
